/*
 * Copyright (c) 2017 Qunar.com. All Rights Reserved.
 */
package com.guoyu.project.web.api;

import javax.annotation.Resource;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.model.enums.ArticleType;
import com.guoyu.project.model.enums.ProjectType;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Article;
import com.guoyu.project.service.ArticleService;

import java.util.List;
import java.util.Map;

/**
 * @author: libo Date: 17-2-13 Time: 下午1:20
 * @version: \$Id$
 */
@Controller
@RequestMapping("api/article")
public class ArticleApiController {

    @Resource
    private ArticleService articleService;

    @RequestMapping("info")
    @ResponseBody
    public APIResponse info(@RequestParam(defaultValue = "0") int articleId) {
        Article article = articleService.queryById(articleId);
        if (article == null) {
            return APIResponse.returnFail("系统不存在ID为:" + articleId + "的记录");
        }
        ArticleType majorType = article.getMajorType();
        Map<String, Object> params = Maps.newHashMap();
        params.put("majorType", article.getMajorType());
        params.put(Pagination.OFFSET, 0);
        params.put(Pagination.LIMIT, 1);

        if (majorType == ArticleType.PROJECT) {
            params.put("minorType", article.getMinorType());

            ProjectType projectType = ProjectType.valueOf(article.getMinorType());

            if (projectType == ProjectType.NEWS) {
                params.put("projectId", article.getProjectId());
            }
        }

        // 查找下一篇
        params.put("nextOrder", article.getId());
        params.put("orderById", "asc");
        List<Article> articleList1 = articleService.queryArticles(params);
        if (CollectionUtils.isNotEmpty(articleList1)) {
            article.setNextId(articleList1.get(0).getId());
        }

        // 查找上一篇
        params.remove("nextOrder");
        params.put("previousOrder", article.getId());
        params.put("orderById", "desc");
        List<Article> articleList2 = articleService.queryArticles(params);
        if (CollectionUtils.isNotEmpty(articleList2)) {
            article.setPreviousId(articleList2.get(0).getId());
        }
        return APIResponse.returnSuccess(article);
    }

    @RequestMapping("recommend")
    @ResponseBody
    public APIResponse recommend() {
        // 推荐内容
        Map<String, Object> recommendParams = Maps.newHashMap();
        recommendParams.put("displayDetail", Boolean.TRUE);
        List<Article> recommendList = articleService.queryArticles(recommendParams);
        return APIResponse.returnSuccess(recommendList);
    }

}
